package com.dianxun.exam.common.base;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;

import com.dianxun.exam.common.ActivityManager;
import com.dianxun.exam.utils.StatusBarUtil;
import com.dianxun.exam.utils.ToastUtil;

import butterknife.ButterKnife;

/**
 * base activity
 */
public abstract class BaseActivity extends AppCompatActivity {
    protected Activity self;
    protected ActivityManager activityManager;
    public Context mContext;

    /**
     * 返回布局显示ID
     *
     * @return 布局id
     */
    protected abstract int getContentView();

    /**
     * 初始化View（findView替代findViewById）
     */
    protected abstract void initView();

    /**
     * 初始化监听
     */
    protected abstract void initListeners();
    public String TAG = "TAG";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        self = this;
        mContext = this;
        TAG=mContext.getClass().getSimpleName();
        if (getContentView() != 0) {
            setContentView(getContentView());
            ButterKnife.bind(this);
            initView();
            initListeners();
        }
        // activity管理器
        activityManager = ActivityManager.getScreenManager();
        activityManager.pushActivity(this);
    }

    /**
     * @desc   沉浸式主题 （两个方法用一个）
     * @author taoli
     * @time 2017/5/26 0026
     */
    public void setLightBar(int colorId){
        //状态栏设置标题颜色
        //StatusBarUtil.setStatusBarColor(this,colorId);
        //状态栏白色主题
        StatusBarUtil.StatusBarLightMode(this,colorId);
    }

    protected final <T extends View> T findView(int id) {
        return (T) super.findViewById(id);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        activityManager = ActivityManager.getScreenManager();
        activityManager.pushActivity(this);
    }

    @Override
    protected void onDestroy() {
        activityManager.popActivity(this);
        super.onDestroy();
    }

    @Override
    public void onAttachFragment(Fragment fragment) {
        super.onAttachFragment(fragment);
    }

    /**
     * 方法名称：jumpActivity
     * 方法描述：启动相关的界面. 不带bundle
     */
    public void jumpActivity(Class<? extends Activity> toActivity) {
        this.jumpActivity(toActivity, null);
    }

    /**
     * 方法名称：jumpActivity
     * 方法描述：启动相关的界面. 带bundle
     */
    public void jumpActivity(Class<? extends Activity> toActivity, Bundle bundle) {
        Intent intent = new Intent(mContext, toActivity);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    /**
     * 跳转回调，用onActivityResultWithoutLogin接收,不带bundle
     *
     * @param reqcode
     * @param toActivity
     */
    public void jumpActivityForResult(int reqcode, Class<? extends Activity> toActivity) {
        this.jumpActivityForResult(reqcode, toActivity, null);
    }

    /**
     * 跳转回调，用onActivityResultWithoutLogin接收,带bundle
     *
     * @param reqcode
     * @param toActivity
     * @param bundle
     */
    public void jumpActivityForResult(int reqcode, Class<? extends Activity> toActivity, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(mContext, toActivity);
        if (null != bundle) {
            intent.putExtras(bundle);
        }
        startActivityForResult(intent, reqcode);
    }

    /**
     * 无动画结束activity
     */
    public void noAnimFinish() {
        super.finish();
        this.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
    }

    @Override
    public void finish() {
        super.finish();
    }

    public void toast(String msg) {
        ToastUtil.toast(msg);
    }

    //公用返回点击事件
    public void backfinish(View view) {
        finish();
    }
}